package com.jichangxiu.framework.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseMapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.jichangxiu.common.constant.Constants;
import com.jichangxiu.common.context.JcxContext;
import com.jichangxiu.common.entity.bo.PageEntity;
import com.jichangxiu.common.enums.OrderType;
import com.jichangxiu.common.utils.ServletUtils;
import com.jichangxiu.common.utils.StrUtils;
import com.jichangxiu.framework.service.BaseService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class BaseServiceImpl<M extends MPJBaseMapper<T>, T, V> extends MPJBaseServiceImpl<M, T> implements BaseService<T, V> {

    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    private static final String PARAM_PAGE_NUM = Constants.JCX_PROPERTIES.getServerProperties().getPageNumToServerKey();

    private static final String PARAM_PAGE_SIZE = Constants.JCX_PROPERTIES.getServerProperties().getPageSizeToServerKey();

    private static final String PARAM_PAGE_ORDER_BY = Constants.JCX_PROPERTIES.getServerProperties().getOrderByToServerKey();

    private static final String PARAM_PAGE_ORDER_TYPE = Constants.JCX_PROPERTIES.getServerProperties().getOrderTypeToServerKey();

    // 设置请求分页数据
    protected <E> Page<E> startPage() {
        PageEntity pageEntity = new PageEntity();
        pageEntity.setPageNum(Convert.toLong(ServletUtils.getParameter(PARAM_PAGE_NUM, String.valueOf(PageEntity.DEFAULT_PAGE_NUM)), PageEntity.DEFAULT_PAGE_NUM));
        pageEntity.setPageSize(Convert.toLong(ServletUtils.getParameter(PARAM_PAGE_SIZE, String.valueOf(PageEntity.DEFAULT_PAGE_SIZE)), PageEntity.DEFAULT_PAGE_SIZE));
        pageEntity.setOrderBy(ServletUtils.getParameter(PARAM_PAGE_ORDER_BY, ""));
        String orderType = ServletUtils.getParameter(PARAM_PAGE_ORDER_TYPE);
        boolean isAsc = !StrUtils.isEmpty(orderType) && (OrderType.ASC.name().equalsIgnoreCase(orderType));
        pageEntity.setIsAsc(isAsc);
        return pageEntity.build();
    }

    protected <E> Page<E> buildPage(long pageNum, long pageSize, long total, List<E> list) {
        Page<E> newPage = new Page<>(pageNum, pageSize, total);
        newPage.setRecords(list);
        return newPage;
    }

    protected <E> Page<E> buildPage(Page<?> page, List<E> list) {
        return buildPage(page.getCurrent(), page.getSize(), page.getTotal(), list);
    }

    protected long countWrapper(Wrapper<T> wrapper) {
        return count(wrapper);
    }

    protected <SU> SU getSecurityUser(Class<SU> cs) {
        return JcxContext.getSecurityUser(cs);
    }

    protected String getTenantId() {
        return JcxContext.getTenantId();
    }

    protected String getTenantName() {
        return JcxContext.getTenantName();
    }

    protected String getTenantSource() {
        return JcxContext.getTenantSource();
    }

    protected String getDeptId() {
        return JcxContext.getDeptId();
    }

    protected String getDeptName() {
        return JcxContext.getDeptName();
    }

    protected String getUserId() {
        return JcxContext.getUserId();
    }

    protected String getUserCode() {
        return JcxContext.getUserCode();
    }

    protected String getUsername() {
        return JcxContext.getUserName();
    }

    protected List<String> getPostCodeList() {
        return JcxContext.getPostCodeList();
    }

    protected List<String> getRoleCodeList() {
        return JcxContext.getRoleCodeList();
    }

    protected List<String> getPowerCodeList() {
        return JcxContext.getPowerCodeList();
    }

    protected String getJti() {
        return JcxContext.getJti();
    }

    protected String getJwt() {
        return JcxContext.getJwt();
    }

}
